/* 
** Copyright (C) 2006  sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei3@gmail.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/

#include "header.h"
#include "mainwindow.h"
#include "menubar.h"
#include "channel.h"
#include "pageplay.h"
#include "tabwidget.h"
#include "fork.h"

PagePlay::PagePlay(QWidget * parent, const char *name):QVBox(parent, name),
tabwidget(static_cast < TabWidget * >(parent))
{
    ////row
    QHBox *hbox = new QHBox(this);

    QToolButton *buttonlaunch = new QToolButton(hbox);
    buttonlaunch->setTextLabel(tr("Launch"));
    buttonlaunch->setAutoRaise(true);
    buttonlaunch->setUsesTextLabel(true);
//    buttonlaunch->setMaximumSize(buttonplayer->sizeHint());
    connect(buttonlaunch, SIGNAL(clicked()), this,
	    SLOT(onButtonLaunchClicked()));

    buttonplayer = new QToolButton(hbox);
    buttonplayer->setTextLabel(tr("Player"));
    buttonplayer->setAutoRaise(true);
    buttonplayer->setUsesTextLabel(true);
    buttonplayer->setToggleButton(TRUE);
//    buttonplayer->setMaximumSize(buttonplayer->sizeHint());
    connect(buttonplayer, SIGNAL(toggled(bool)), this,
	    SLOT(onButtonPlayerToggled(bool)));

    buttonstop = new QToolButton(hbox);
    buttonstop->setTextLabel(tr("Stop"));
    buttonstop->setAutoRaise(true);
    buttonstop->setUsesTextLabel(true);
//    buttonstop->setMaximumSize(buttonstop->sizeHint());
    connect(buttonstop, SIGNAL(clicked()), this,
	    SLOT(onButtonStopClicked()));

    QToolButton *but = new QToolButton(hbox);
    but->setTextLabel(tr("x"));
    but->setAutoRaise(true);
    but->setUsesTextLabel(true);
    but->setMaximumSize(but->sizeHint());
    connect(but, SIGNAL(clicked()), this, SLOT(removeTab()));

    ///row fork
    fork = new Fork(tabwidget, this);

    flagStopSop=FALSE;
}


void PagePlay::onButtonStopClicked()
{
    fork->killSop();
    fork->killPlayer();
    flagStopSop=TRUE;
}

void PagePlay::onButtonPlayerToggled(bool t)
{
    if (t) {
	if (fork->snstatus && !fork->player->isRunning()) {
	    if (fork->forkPlayer() == TRUE)
		buttonplayer->setPaletteForegroundColor(Qt::blue);
	    else
		fprintf(stderr, "failed to launch player!\n");
	}
    } else
	fork->killPlayer();
}

void PagePlay::removeTab()
{
    if (tabwidget->count() == 1)
	return;

    PagePlay *pageplay =
	static_cast < PagePlay * >(tabwidget->currentPage());
    tabwidget->removePage(pageplay);
    delete pageplay;
}


void PagePlay::onButtonLaunchClicked()
{
    QListViewItem *item =
	static_cast < MainWindow * >(tabwidget->parentWidget())
	->channel->currentItem();
    if (!item) {
	return;
    }
    if (item->depth() != 1) {
	return;
    }

    fork->killSop();
    fork->killPlayer();

    if (fork->
	forkSop(item->text(URL_COLUMN), item->text(NAME_COLUMN),
		item->text(STREAM_COLUMN))
	== TRUE) {
	///add to history
	static_cast < MainWindow * >(tabwidget->parentWidget())
	    ->menubar->addToPopupMenu(static_cast < MainWindow * >
				      (tabwidget->parentWidget())->
				      menubar->history,
				      item->text(URL_COLUMN),
				      item->text(NAME_COLUMN),
				      item->text(STREAM_COLUMN));
    } else
	fprintf(stderr, "failed to launch sp-sc!\n");
}
